import os
import  shutil
# 只获取最底层的字文件路径
def get_subfolder_paths(root_dir, crop_path = '100-images'):
    subfolder_paths = []
    for dirpath, dirnames, filenames in os.walk(root_dir):
        if not dirnames:
            full_path = os.path.abspath(dirpath)
            # 对绝对路径进行裁剪
            split_path = full_path.split(crop_path)[1]
            last_part = os.path.basename(split_path)
            if last_part != 'labels':
                subfolder_paths.append(split_path)

    return subfolder_paths

def get_img_file_name(path):
    file_names = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.lower().endswith('.jpg'):
                file_name = os.path.splitext(file)[0]
                file_names.append(file_name)
    return file_names


'''
    将A路径下所有的txt文件 转移到 A路径的上一级路径下的 lables
'''
if __name__ == '__main__':
    tmp_name = '102-images'
    # 获取子目录的路径
    root = "/home/yjh_test/data/helmet_data/HelmetDataset-zhengshu-20241121/" + tmp_name
    crop_path = tmp_name
    sub_path_names = get_subfolder_paths(root, crop_path)
    # 测试
    # for path in sub_path_names:
    #     print(path)
    # exit(0)
    # A路径
    A_path = '/home/yjh_test/data/helmet_data/HelmetDataset-zhengshu-20241121/' + tmp_name

    for tmp_path in sub_path_names:
        A_path_1 = A_path + tmp_path
        B_path = os.path.dirname(A_path_1) + '/labels'
        if not os.path.exists(B_path):
            os.makedirs(B_path)
        # 将A下面的所有txt转移到B下面
        number = 0
        for item in os.listdir(A_path_1):
            if os.path.isfile(os.path.join(A_path_1, item)) and item.endswith('.txt'):
                src_file_path = os.path.join(A_path_1, item)
                shutil.move(src_file_path, B_path)
                number += 1
        print("Path {} move finish !! number {} ".format(A_path_1, number))
        # exit(0)
    print("All folder move succeed !!!")



